import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:live_music/controller/video_list_controller.dart';
import 'package:live_music/event/videoListEvent.dart';
import 'package:live_music/models/moment_detail_model.dart';
import 'package:live_music/models/moment_list_model.dart';
import 'package:live_music/pages/video/index.dart';
import 'package:live_music/pages/video/single_video_index.dart';
import 'package:live_music/services/momentService.dart';
import 'package:live_music/services/squareService.dart';
import 'package:live_music/services/userService.dart';
import 'package:live_music/utils/eventBusUtils.dart';

///视频列表页
class SingleVideoListPage extends StatefulWidget {
  SingleVideoListPage(
      {Key key, this.userId, this.momentListModel, this.pageNo, this.index})
      : super(key: key);
  int userId;
  int index;
  List<MomentDetailModel> momentListModel;
  int pageNo;
  @override
  _SingleVideoListPageState createState() {
    return _SingleVideoListPageState();
  }
}

class _SingleVideoListPageState extends State<SingleVideoListPage>
    with AutomaticKeepAliveClientMixin {
  PageController _pageController;
  int pageNo = 1;
  int pageSize = 15;
  bool hasMore = true;
  int userId;
  List<MomentDetailModel> _momentListModel;
  @override
  void initState() {
    super.initState();
    _pageController = PageController(keepPage: true, initialPage: widget.index);
    setState(() {
      userId = widget.userId;
      pageNo = widget.pageNo;
      _momentListModel = widget.momentListModel;
    });
    initData();
  }

  initData() async {
    await loadCurAboutIdData();
    EventBusUtils.getInstance().on<CurVideoListIndexEvent>().listen((event) {
      if (_momentListModel.length < event.index + 3) {
        if (hasMore) {
          loadCurAboutIdData(replace: false);
        }
      }
    });
  }

  loadCurAboutIdData({bool replace = true}) async {
    Map<String, dynamic> data = {
      "pageNo": pageNo,
      "pageSize": pageSize,
      "userId": userId
    };
    Map<String, dynamic> result = await UserService.queryCollectVideo(data);
    List<dynamic> momentList = result['body'];
    MomentListModel momentListModel = MomentListModel.fromJson(momentList);
    setState(() {
      pageNo++;
      hasMore = pageSize == momentListModel.list.length;
      // if (replace) {
      //   _momentListModel = momentListModel.list;
      // } else {
      _momentListModel.addAll(momentListModel.list);
      // }
    });
  }

  @override
  void dispose() {
    super.dispose();
    _pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Brightness.light,
    ));
    return Scaffold(
        backgroundColor: Colors.black,
        body: Column(children: [
          Container(
            height: MediaQuery.of(context).size.height,
            child: Stack(
              children: [
                _getVideoPageView(),
                // _getTopBarLayout(),
              ],
            ),
          ),
        ]));
  }

  _getTopBarLayout() {
    return Container(
      height: 40,
      constraints: BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: 40,
          minHeight: 40),
      margin:
          EdgeInsets.only(top: MediaQueryData.fromWindow(window).padding.top),
      child: Stack(
        alignment: Alignment.center,
        children: [
          Positioned(
            left: 0,
            child: IconButton(
              icon: Icon(
                Icons.arrow_back_ios_rounded,
                color: Colors.white,
              ),
              onPressed: () {
                Get.back();
              },
            ),
          ),
          Positioned(
              right: 0,
              child: IconButton(
                icon: Icon(
                  Icons.search_rounded,
                  color: Colors.white,
                ),
              ))
        ],
      ),
    );
  }

  _getVideoPageView() {
    double contentHeight = MediaQuery.of(context).size.height -
        48 -
        MediaQueryData.fromWindow(window).padding.top;
    return Container(
        constraints: BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: contentHeight,
        ),
        margin:
            EdgeInsets.only(top: MediaQueryData.fromWindow(window).padding.top),
        child: PageView.builder(
          controller: _pageController,
          itemCount: _momentListModel.length,
          scrollDirection: Axis.vertical,
          itemBuilder: (context, index) {
            return SingleVideoIndexPage(
                index: index, videoItem: _momentListModel[index]);
          },
        ));
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
